<?php

Class Controller_AuxPacients Extends Controller_Base {

	

	function Index(){
		
		$this->template->show('pacients');

	}


	function getPacients(){
		if($_POST['buscar']=='nhc'){
			$pacient = new Pacients();
			$c = new Criteria();
			$c->Add(PacientsPeer::NHC,$_POST['nhc']);
			$c->Add(PacientsPeer::ASSIGNAT,'S');
			$Pacients = PacientsPeer::doSelect($c);
		}else if($_POST['buscar']=='epi'){
			$pacient = new Pacients();
			$c = new Criteria();
			$c->Add(PacientsPeer::NUM_VISITA,$_POST['epi']);
			$c->Add(PacientsPeer::ASSIGNAT,'S');
			$Pacients = PacientsPeer::doSelect($c);
		}else{
			echo 'KO';
		}
		
		if($Pacients){
			$res.='<table class="table1">
			<tr><th colspan="3">'.TXT_RESULTAT_BUSQUEDA.'</th></tr>';
			foreach($Pacients as $pacient){
				$res.='<tr><td>'.$pacient->getCognoms().', '.$pacient->getNom().'</td>';
				$res.='<td>'.$pacient->getDataVisita('%d/%m/%Y').'</td>';
				$res.='<td width="15%"><a href="index.php?route=auxPacients/anulaVisita&id='.$pacient->getId().'" 
						onclick="return confirm('.TXT_BORRAR.')"><img  src="templates/images/cancel.png" 
						title="anula"/>&#160;anula visita</a></td></tr>';				
			}
			$res.='</table>';
		}else{
			$res='<b style="color:red">'.TXT_NO_RESULTATS.'</b>';
		}
		echo $res;
	}
	
	
	function anulaVisita(){
		try {
			//Enviar XML D'ANULACIÓ
			$p = new Pacients();
			$p=PacientsPeer::retrieveByPK($_GET['id']);
			$p->setAssignat('N');
			$p->save();
			//escriu el xml
				try {
				$wl = new WorkList();
				$wl->escriuMissatgeAnulacio($p);
				}
				catch (Exception $i){

					//reactivem el passient
					$p->setAssignat('S');
					$p->save();
					
					$this->template->remove('error_code');
					$this->template->set('error_code', $i->getMessage());
					$this->template->show('error');
					exit();
				}
			
			
			
			$this->template->set('idVisita',$_GET['id']);
			$this->template->set('anulat','true');				
			$this->Index();
		}catch (TemplateException $e){
			$this->template->remove('error_code');
			$this->template->set('error_code', $e->getMessage());
			$this->template->show('error');
		}
	}
	
}

?>